package com.ai.chat.controller;

import java.util.List;
import com.ai.chat.domain.base.BaseAgent;
import com.ai.chat.domain.base.BaseAgentConfig;
import com.ai.chat.domain.base.BaseFile;
import com.ai.chat.domain.base.BaseHistory;
import com.ai.chat.domain.base.request.BaseChatRequest;
import com.ai.chat.domain.base.request.BaseQuery;
import com.ai.chat.service.OllamaService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/ollama")
public class OllamaController {
    @Resource
    private OllamaService ollamaService;

    @GetMapping("/config")
    public BaseAgentConfig config() {
        BaseAgentConfig config = new BaseAgentConfig();
        config.setName("deepseek");
        config.setPrompt("我是你的AI智能助理，你可以问我任何问题");
        config.setStreamFlag(true);
        config.setNetQueryFlag(true);
        config.setAttachFlag(true);
        config.setDeepThinkFlag(true);
        config.setMultipleAgentFlag(true);
        config.setHistoryFlag(true);
        return config;
    }

    @PostMapping("/chat")
    public String chat(@RequestBody BaseChatRequest request) {
        return ollamaService.chat(request);
    }

    @PostMapping("/streamChat")
    public Flux<String> streamChat(@RequestBody BaseChatRequest request, HttpServletResponse response) {
        return ollamaService.streamChat(request, response);
    }

    @PostMapping("/file")
    public BaseFile file(@RequestParam("file") MultipartFile file) {
        return ollamaService.uploadFile(file);
    }

    @DeleteMapping("/streamChat")
    public void disposeStream(String streamId) {
        ollamaService.disposeStream(streamId);
    }

    @PostMapping("/agentList")
    public List<BaseAgent> getAgentList(@RequestBody BaseQuery query) {
        return ollamaService.getAgentList(query);
    }

    @PostMapping("/historyList")
    public List<BaseHistory> getHistoryList(@RequestBody BaseQuery query) {
        return ollamaService.getHistoryList(query);
    }

    @PostMapping("/history")
    public BaseHistory createHistory(@RequestBody BaseHistory history) {
        return ollamaService.createHistory(history);
    }

    @PutMapping("/history")
    public Boolean updateHistory(@RequestBody BaseHistory history) {
        return ollamaService.updateHistory(history);
    }

    @DeleteMapping("/history")
    public Boolean deleteHistory(String id) {
        return ollamaService.deleteHistory(id);
    }
}
